From: Simon McVittie <smcv@debian.org>
Date: Thu, 7 May 2020 12:13:56 +0100
Subject: build-db: Use the same database format that the C code expects

Otherwise, Python 2 anydbm will preferentially choose Berkeley DB format
(dbhash/bsddb), which is neither GNU gdbm nor traditional Unix (n)dbm.

Signed-off-by: Simon McVittie <smcv@debian.org>
Fixes: https://github.com/lathiat/avahi/issues/260
---
 configure.ac                      | 11 ++++++++---
 service-type-database/Makefile.am |  4 ++--
 service-type-database/build-db    | 27 +++++++++++++++++++++------
 3 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/configure.ac b/configure.ac
index 58db8c7..5aea4e7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -871,9 +871,14 @@ if test "x$HAVE_PYTHON" = "xyes" ; then
         fi
 
         AM_CHECK_PYMOD(socket,,,[AC_MSG_ERROR(Could not find Python module socket)])
-        if test "x$HAVE_GDBM" = "xyes" || test "x$HAVE_DBM" = "xyes"; then
-            AM_CHECK_PYMOD(anydbm,,,[
-                AM_CHECK_PYMOD(dbm,,,[AC_MSG_ERROR(Could not find Python module dbm)])
+        if test "x$HAVE_GDBM" = "xyes"; then
+            AM_CHECK_PYMOD([dbm.gnu], [], [], [
+                AM_CHECK_PYMOD([gdbm], [], [], [AC_MSG_ERROR(Could not find Python module dbm.gnu or gdbm)])
+            ])
+        fi
+        if test "x$HAVE_DBM" = "xyes"; then
+            AM_CHECK_PYMOD([dbm.ndbm], [], [], [
+                AM_CHECK_PYMOD([dbm], [], [], [AC_MSG_ERROR(Could not find Python module dbm.ndbm or dbm)])
             ])
         fi
     fi
diff --git a/service-type-database/Makefile.am b/service-type-database/Makefile.am
index f9fa082..0ead0f6 100644
--- a/service-type-database/Makefile.am
+++ b/service-type-database/Makefile.am
@@ -28,7 +28,7 @@ noinst_SCRIPTS=build-db
 pkglibdata_DATA+=service-types.db
 
 service-types.db: service-types
-	$(AM_V_GEN)$(PYTHON) build-db $< $@.coming && \
+	$(AM_V_GEN)$(PYTHON) build-db --gnu $< $@.coming && \
 	mv $@.coming $@
 
 CLEANFILES = service-types.db
@@ -44,7 +44,7 @@ service-types.db.pag: service-types.db
 service-types.db.dir: service-types.db
 	$(AM_V_GEN)mv service-types.db.coming.dir service-types.db.dir
 service-types.db: service-types build-db
-	$(AM_V_GEN)$(PYTHON) build-db $< $@.coming && \
+	$(AM_V_GEN)$(PYTHON) build-db --ndbm $< $@.coming && \
 	if test -f "$@.coming"; then mv $@.coming $@; fi
 
 CLEANFILES = service-types.db*
diff --git a/service-type-database/build-db b/service-type-database/build-db
index 78ee892..6415d27 100755
--- a/service-type-database/build-db
+++ b/service-type-database/build-db
@@ -17,13 +17,28 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 # USA.
 
-try:
-    import anydbm as dbm
-except ImportError:
-    import dbm
-
 import sys
 
+if sys.argv[1] == '--gnu':
+    if sys.version_info >= (3,):
+        import dbm.gnu as chosen_dbm
+    else:
+        import gdbm as chosen_dbm
+
+    sys.argv[1:] = sys.argv[2:]
+elif sys.argv[1] == '--ndbm':
+    if sys.version_info >= (3,):
+        import dbm.ndbm as chosen_dbm
+    else:
+        import dbm as chosen_dbm
+
+    sys.argv[1:] = sys.argv[2:]
+else:
+    if sys.version_info >= (3,):
+        import dbm as chosen_dbm
+    else:
+        import anydbm as chosen_dbm
+
 if len(sys.argv) > 1:
     infn = sys.argv[1]
 else:
@@ -34,7 +49,7 @@ if len(sys.argv) > 2:
 else:
     outfn = infn + ".db"
 
-db = dbm.open(outfn, "n")
+db = chosen_dbm.open(outfn, "n")
 
 for ln in open(infn, "r"):
     ln = ln.strip(" \r\n\t")
